package com.gs.common.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.ContentType;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component
@Log4j2
public class FileUtils {

    @Value("${file.resourcePath}")
    private String resourcePath;

    /**
     * 获取临时文件路径
     * @return
     */
    public String getTempResourcePath() {
        return resourcePath;
    }

    /**
     * 删除临时文件
     * @param fileName
     * @return
     */
    public Boolean deleteTempFile(String fileName) {
        File file = new File(resourcePath + fileName);
        if(file.isDirectory()) {

        } else {
            return file.delete();
        }
        return false;
    }

    /**
     * 上传文件
     * @param path 目标路径
     * @param base64File Base64数据
     * @return
     */
    public static boolean upload(String path, String base64File) {
        // 去除图片Base64信息的前缀
        base64File = ImageUtils.Base64RemovePrefix(base64File);

        boolean result = ImageUtils.Base64ToImage(base64File, path);
        if(result){
            log.info("上传文件:" + path);
        }else{
            log.error("上传文件失败,目标路径:{}",path);
        }
        return result;

    }

    public static void fileToBrowser(String path, HttpServletResponse response) throws IOException {
        File file = new File(path);
        FileInputStream inputStream = getFile(path);
        byte[] b = new byte[inputStream.available()];
        inputStream.read(b);
        response.addHeader("filename", file.getName());
        response.addHeader("Access-Control-Allow-Headers", "filename");
        response.addHeader("Access-Control-Expose-Headers", "filename");
        IoUtil.write(response.getOutputStream(), true, b);

    }

    public static FileInputStream getFile(String url) {
        File file = new File(url);
        return IoUtil.toStream(file);
    }


    /**
     * 根据时间 + 随机值得到文件名称
     *
     * @return
     */
    public static String getFileNameForDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String newName = format.format(new Date());
        //获得三位随机数
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            newName = newName + random.nextInt(9);
        }
        return newName;
    }

    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date());
    }

    /**
     * 判断文件是不是图片
     *
     * @param fileName
     * @return
     */
    public static boolean checkFileIsImage(String fileName) {
        String[] imageSuffix = new String[]{"jpg", "gif", "bmp", "png", "jpeg", "ico", "jpe", "tiff", "jfif", "tif"};
        String s = fileName.toLowerCase();
        for (String suffix : imageSuffix) {
            if (s.contains("." + suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取文件后缀名
     * @param fileName 文件名
     * @return
     */
    public static String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
    }


    /**
     *
     * @param file
     * @return
     */
    public MultipartFile getMultipartFile(File file){
        FileInputStream fileInputStream = null;
        MultipartFile multipartFile = null;
        try {
            fileInputStream = new FileInputStream(file);
            multipartFile = new MockMultipartFile(file.getName(),file.getName(),
                    ContentType.MULTIPART.toString(),fileInputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return multipartFile;
    }
}
